<?php


namespace app\api\controller\v1;

use app\api\model\BikeAlertModel;
use app\api\model\BikeMgr;
use app\api\model\BikeModel;
use app\api\model\NPAModel;
use app\api\model\ServiceAreaModel;
use app\api\model\UserModel;
use app\api\model\UserRadingOrderModel;
use app\api\model\UserReparkOrderModel;
use app\common\controller\UserBaseController;
use app\common\library\aliyunsms\alidayu;
use app\common\library\AMapModel;
use app\common\library\DLock;
use app\common\library\IDMan;
use app\common\library\WxmpMsg;
use common;
use ApiOper;

class UrorderController extends UserBaseController
{
    function submitAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'reserved' => 'int',
            'user_coordinate' => ['optintarray', [0, 0]],
            'form_id' => 'optstring',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['bike_id'], 30);

        $bike_info = (new BikeModel())->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "该车辆故障,请换一辆骑行");
        }
        $city_id = $bike_info['city_id'];

        $userinfo = (new UserModel())->query_info(ApiOper::instance()->userinfo()['id']);


        $ret = (new UserRadingOrderModel())->submit($userinfo, $bike_info, $this->argv['user_coordinate'], $this->argv['reserved'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];
        $this->resp['bluetooth_info'] = [
            'name' => $bike_info['bluetooth_name'],
            'token' => $bike_info['bluetooth_token'],
        ];

        $alert_type_arr[] = BIKE_ALERT_LONG_NO_BUS;
        (new BikeMgr())->auto_handle_alert($city_id, $bike_info['id'], $alert_type_arr);
        if(!empty($this->argv['form_id']))
            (new WxmpMsg())->send_msg_ride_start($this->argv['form_id'], $userinfo['wxmp_open_id']);


        return $this->ret_resp();
    }

    function finishAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'string',
            'user_coordinate' => ['optintarray', [0, 0]],
            'flush_reason' => ['optint', 0],
            'report_bike_fault_types' => ['optintarray', []],
            'report_bike_fault_descs' => ['optstringarray', []],
            'form_id' => ['optstring', null],
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);

        $order_info = (new UserRadingOrderModel())->query_info_by_city_id($this->city_id, $this->argv['order_id'], true);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败");
        }
        if ($order_info['order_state'] != RIDE_ORDER_STATE_RIDING && $order_info['order_state'] != RIDE_ORDER_STATE_TEMP_PARKING) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单状态不匹配!");
        }

        $bike_info = BikeModel::instance()->get_info_by_id($order_info['bike_id']);
        if ($this->argv['flush_reason'] == FLUSH_ORDER_REASON_BIKE_SICK) {
            (new BikeAlertModel())->add($this->city_id, $bike_info['id'], BIKE_ALERT_FAULT, '用户报告故障:' . implode(",", $this->argv['report_bike_fault_descs']));
        }


        $ret = (new UserRadingOrderModel())->finish($order_info,
            $this->argv['user_coordinate'],
            $this->argv['flush_reason'],
            $this->argv['report_bike_fault_types'],
            $this->argv['bluetooth']);

        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        $this->resp['info'] = $ret[2];

        $order_info = $ret[2];
        $miles = floor($order_info['distance']) . " 米";
        $amount = round($order_info['paid_amount'] / 100, 2) . "元";
        $duration = ceil($order_info['duration'] / 60) . " 分钟";
        $amount_detail = null;
        $userinfo = (new UserModel())->query_info($order_info['user_id']);
        if (!empty($this->argv['form_id'])) {
            if ($amount == 0) {
                $amount_detail = "免费";
            } else {
                $amount_detail = "行程总花费 {$amount}";
            }
            (new WxmpMsg())->send_msg_ride_finish($this->argv['form_id'], $userinfo['wxmp_open_id'], $miles, $duration, $amount, $amount_detail);
        }
        if (ApiOper::instance()->userinfo()['user_type'] != OPER_TYPE_USER) {
            (new alidayu())->send_sms_ride_order_finish($userinfo['phone'], $miles, $duration, $amount);
        }

        return $this->ret_resp();
    }

    function force_finishAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'bike_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "车辆");
        }

        if ($bike_info['bus_state'] <= BIKE_BUS_STAT_RESERVED && $bike_info['bus_state'] >= BIKE_BUS_STAT_TEMP_PARK) {
            $this->throw_myexception(X_ERROR, "车辆不在骑行中！");
        }

        $order_info = (new UserRadingOrderModel())->query_info_by_city_id($this->city_id, $bike_info['last_order_id'], true);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败");
        }

        $ret = (new UserRadingOrderModel())->force_finish($order_info);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function rerideAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'string',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $ret = (new UserRadingOrderModel())->reride($this->city_id, $this->argv['order_id'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            throw new MyException($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];
    }

    function temp_parkAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'string',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $ret = (new UserRadingOrderModel())->park_temp($this->city_id, $this->argv['order_id'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function adjust_amountAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'amount_type' => 'int',
            'amount' => 'int',
            'reason' => 'string',
            'remark' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $ret = (new UserRadingOrderModel())->adjust_amount($this->city_id, $this->argv['order_id'], $this->argv['amount_type'], $this->argv['amount'], $this->argv['reason'], $this->argv['remark']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'optstring',
            'order_state' => 'optint',
            'is_start_from_park' => 'optint',
            'start_park_state' => 'optint',
            'is_amount_adjusted' => 'optint',
            'is_flush' => 'optint',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'start_time' => "optdatetime",
            'end_time' => "optdatetime",
        );

        $this->check_params();
        $this->check_token();

        $cd = $this->argv;

        $month = 90;
        if (!empty($this->argv['sk'])) {
            if (common::is_phone($this->argv['sk'])) {
                $usrinfo = (new UserModel())->query_info_by_phone($this->argv['sk']);
                if (!empty($usrinfo)) {
                    $cd['user_id'] = $usrinfo['id'];
                }
                $month = 90;
            } else if (IDMan::is_bike_sn($this->argv['sk'])) {
                $bike_info = (new BikeModel())->get_info_by_sn($this->argv['sk']);
                if (!empty($bike_info)) {
                    $cd['bike_id'] = $bike_info['id'];
                }
                $month = 90;
            }
        }

        if (!isset($this->argv['end_time'])) {
            $this->argv['end_time'] = date('Y-m-d H:i:s');
        }

        $end_time = strtotime($this->argv['end_time']);

        if (!isset($this->argv['start_time'])) {
            $this->argv['start_time'] = date('Y-m-d H:i:s', $end_time - 86400 * $month);
        }

        $start_time = strtotime($this->argv['start_time']);

        if ($end_time - $start_time > 3600 * 24 * $month) {
            $this->argv['start_time'] = date('Y-m-d H:i:s', $end_time - 86400 * $month);
        }

        $list = (new UserRadingOrderModel())->query_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();

        return $this->ret_resp();
    }

    function list_by_skAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sk' => 'string',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();
        $list = [];
        $total = 0;
        $cd = [];
        $city_id = null;
        if (!empty($this->argv['sk'])) {
            if (common::is_phone($this->argv['sk'])) {
                $usrinfo = (new UserModel())->query_info_by_phone($this->argv['sk']);
                if (!empty($usrinfo)) {
                    $cd['user_id'] = $usrinfo['id'];
                    $city_id = $usrinfo['city_id'];
                }
            } else if (IDMan::is_bike_sn($this->argv['sk'])) {
                $bike_info = (new BikeModel())->get_info_by_sn($this->argv['sk']);
                if (!empty($bike_info)) {
                    $cd['bike_id'] = $bike_info['id'];
                    $city_id = $bike_info['city_id'];
                }
            }
        }
        if ($city_id) {
            $list = (new UserRadingOrderModel())->query_list($city_id, $cd, $this->argv['pos'], $this->argv['limit']);
            $total = hisql::query_count();
        }
        $this->resp['list'] = $list;
        $this->resp['total'] = $total;

        return $this->ret_resp();
    }

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $info = (new UserRadingOrderModel())->query_info_with_adjust_amount_record($this->city_id, $this->argv['order_id']);
        if (ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_USER) {
            //$info['track_grasproad'] = $info['track'];
        }

        if (!empty($info)){
            $info['is_can_repark'] = 1;

            if ($info['order_state'] != RIDE_ORDER_STATE_FINISH) {
                $info['is_can_repark'] = 0;
            }

            if (!empty($info['end_svca_id'])){
                $info['is_can_repark'] = 0;
            }

            if (!empty($info['user_repark_order'])){
                $info['is_can_repark'] = 0;
            }

        }

        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function trackAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new UserRadingOrderModel())->query_track($this->city_id, $this->argv['order_id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function grasproad_trackAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'string',
        );

        $this->check_params();
        //$this->check_token();

        $info = (new UserRadingOrderModel())->query_track($this->city_id, $this->argv['order_id']);
        if (empty($info)) {
            $this->throw_myexception(X_ERROR, "查询订单轨迹失败!");
        }

        $track_new = (new AMapModel())->grasproad($info['track']);
        if (empty($track_new)) {
            $this->throw_myexception(X_ERROR, "订单={$this->argv['order_id']},轨迹纠偏失败!");
        }

        (new UserRadingOrderModel())->set_grsproad_track($this->city_id, $this->argv['order_id'], $track_new);

        return $this->ret_resp();
    }

    function my_riding_orderAction()
    {
        $this->paramlist = array(
            'token' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'user_id' => ApiOper::instance()->userinfo()['id'],
            'is_order_finished' => 0,
        ];

        $list = (new UserRadingOrderModel())->query_list(null, $cd, 0, 1);
        $this->resp['info'] = empty($list) ? null : $list[0];

        if (!empty($list)) {
            $bike_info = BikeModel::instance()->get_info_by_id($list[0]['bike_id']);
            $this->resp['bluetooth_info'] = [
                'name' => $bike_info['bluetooth_name'],
                'token' => $bike_info['bluetooth_token'],
            ];
        }

        return $this->ret_resp();
    }

    function my_paying_orderAction()
    {
        $this->paramlist = array(
            'token' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'user_id' => ApiOper::instance()->userinfo()['id'],
            'is_order_finished' => 1,
            'is_pay' => 0,
        ];

        $list = (new UserRadingOrderModel())->query_paying_list(null, $cd, 0, 1);
        $this->resp['info'] = empty($list) ? null : $list[0];

        return $this->ret_resp();
    }

    function myordersAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'user_id' => ApiOper::instance()->userinfo()['id'],
            'is_order_finished' => 1,
        ];

        $list = (new UserRadingOrderModel())->query_list(null, $cd, $this->argv['pos'], $this->argv['limit']);

        $total = hisql::query_count();

        $num = count($list);

        for ($x=0; $x<$num; $x++){
            $list[$x]['is_can_repark'] = 0;

            if ($list[$x]['order_state'] != RIDE_ORDER_STATE_FINISH) {
                continue;
            }

            if (!empty($list[$x]['end_svca_id'])){
                continue;
            }

            $now = time();
            if (time() - strtotime($list[$x]['end_time']) > 600){
                continue;
            }

            if (!empty($list[$x]['user_repark_order'])){
                continue;
            }

            $list[$x]['is_can_repark'] = 1;
        }

        $this->resp['list'] = $list;
        $this->resp['total'] =$total;

        return $this->ret_resp();
    }

    function query_fee_infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'order_id' => 'string',
            'tip' =>'optint',
        );


        $this->check_params();
        $this->check_token();


        $order_info = (new UserRadingOrderModel())->query_info_by_city_id($this->city_id, $this->argv['order_id'], true);
        if (empty($order_info)) {
            $this->throw_myexception(X_ERROR, "查询订单失败!");
        }

        if ($order_info['order_state'] < RIDE_ORDER_STATE_RIDING || $order_info['order_state'] >= RIDE_ORDER_STATE_ARRIVE) {
            // throw_myexception(ERROR_STATE_DISMATCH, "订单已完成！");
        }

        $bike_info = BikeModel::instance()->get_info_by_id($order_info['bike_id']);

        $duration = time() - strtotime($order_info['start_time']);

        //暂时泰比特去查询一下
        (new BikeMgr())->query_bike_tbt($bike_info);

        $svcinfo = (new ServiceAreaModel())->is_in($bike_info['city_id'], $bike_info['coordinate']);

        $npainfo = (new NPAModel())->is_in($bike_info['city_id'], $bike_info['coordinate']);
        $order_info = array_merge($order_info, [
            'end_svca_id' => !empty($svcinfo) ? $svcinfo['id'] : null,
            'end_npa_id' => !empty($npainfo) ? $npainfo['id'] : null,
            'duration' => $duration,
        ]);

        $fee_info = (new UserRadingOrderModel())->calc_amount($order_info);

        //提示声音
        if (isset($this->argv['tip'])){
            //
            $tip = $this->argv['tip'];
            if($tip == 1&&($fee_info['npa_fee'] + $fee_info['osvc_fee'] > 0)){
                //
                $bound_order_info = [
                    'order_type' => ORDER_TYPE_USER_RIDE,
                    'order_id' => $this->argv['order_id'],
                    'op' => '禁停区警告',
                ];
                (new BikeMgr())->device_voice($order_info['imei'],ECU_VOICE_TYPE_PARKWARN,$bound_order_info);

                if (!empty($npainfo)){
                    if ($npainfo['is_no_park'] == 1){
                        $fee_info['remark'] = '该区域无法结束订单,'.$fee_info['remark'];
                    }
                }
            }
        }

        $this->resp['fee_info'] = $fee_info;

        return $this->ret_resp();
    }

    function price_ruleAction()
    {
        $this->paramlist = [
            'bike_id' => 'optstring',
            'city_id' => 'optstring',
            'svca_id' => 'optstring',
        ];

        $this->check_params();

        $bike_id = "";
        if (isset($this->argv['bike_id'])) {
            $bike_id = $this->argv['bike_id'];
        }

        $city_id = $this->argv['city_id'];
        $svca_id = $this->argv['svca_id'];

        if (!empty($bike_id)){
            $bike_info = BikeModel::instance()->get_info_by_id($bike_id);
            if ($bike_info){
                $city_id = $bike_info['city_id'];
                $svca_id = $bike_info['svca_id'];
            }
        }

        $price_rule = (new UserRadingOrderModel())->get_price_rule($city_id, $svca_id);
        $this->resp['price_rule'] = $price_rule;

        if (empty($city_id)){
            $this->resp['ride_card'] = 0;
        }
        else{
            if ($city_id == '350800'){
                $this->resp['remark'] = '骑行卡更加优惠,单次低至0.9元';
            }
            else if ($city_id == '451100'){
                $this->resp['remark'] = '骑行卡更加优惠,单次低至0.6元';
            }
            else if ($city_id == '350002'){
                $this->resp['remark'] = '骑行卡更加优惠,单次低至0.9元';
            }
            else if ($city_id == '450924'){
                $this->resp['remark'] = '骑行卡更加优惠,单次低至0.8元';
            }
            else{
                $this->resp['remark'] = '骑行卡更加优惠哟';
            }


            if ($city_id  == '451100' || $city_id == '350800'||$city_id =='350001' ||$city_id =='350002'||$city_id == '450924'){
                $this->resp['ride_card'] = 1;
            }
            else{
                $this->resp['ride_card'] = 0;
            }
        }


        return $this->ret_resp();

    }

    //修改价格
    function modify_priceAction(){
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'fee_info' => 'array',
            'reason' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $ret = (new UserRadingOrderModel())->modify_price(null, $this->argv['order_id'], $this->argv['fee_info'], $this->argv['reason']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        return [0,$ret];
    }

    //获取具体费用
    function query_order_feeinfoAction(){
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $order_info = (new UserRadingOrderModel())->query_fee_info($this->city_id, $this->argv['order_id']);
        if (empty($order_info)) {
            $this->throw_myexception(X_ERROR, "查询订单失败!");
        }


        if ($order_info['order_state'] != RIDE_ORDER_STATE_FINISH) {
            $this->throw_myexception(X_ERROR, "订单未结束!");
        }

        if ($order_info['is_pay'] == 1) {
            $this->throw_myexception(X_ERROR, "已支付订单无法修改价格!");
        }



        $base_fee = $order_info['fee_info']['no_coupon_fee'] -$order_info['fee_info']['mile_fee'] - $order_info['fee_info']['duration_fee'];

        $fee_info =[
            'basic_fee' => $base_fee,      //起步价
            'mile_fee' =>$order_info['fee_info']['mile_fee'],   //里程费
            'duration_fee' =>$order_info['fee_info']['duration_fee'],    //时长费
            'npa_fee' =>$order_info['fee_info']['npa_fee'],     //禁停区费用,
            'osvc_fee' =>$order_info['fee_info']['osvc_fee'],   //服务区外费用
        ];

        $info =[
            'id' =>$order_info['id'],
            'fee_info' =>$fee_info,
        ];
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }


    //用户挪车
    function user_repark_submitAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'order_id' => 'string',
            'user_coordinate' => 'floatarray',
            'bluetooth' => ['optint', 0],
            'unlock' => ['optint', 1],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['bike_id'], 60);
        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败!");
        }

        if ($bike_info['last_order_id'] != $this->argv['order_id']){
            $this->throw_myexception(X_ERROR, "该车辆已经被他人骑行,无法挪车!");
        }

        $order_count = (new UserRadingOrderModel())->get_user_unfinished_order(ApiOper::instance()->userinfo()['id']);
        if ($order_count > 0) {
            $this->throw_myexception(X_ERROR, "您有进行中的骑行的订单!");
        }

        $order_info = (new UserRadingOrderModel())->query_info_by_city_id($this->city_id, $this->argv['order_id'],false);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }

        if ($order_info['order_state'] != RIDE_ORDER_STATE_FINISH) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单未结束!");
        }

        if (!empty($order_info['end_svca_id'])){
            $this->throw_myexception(ERROR_STATE_DISMATCH, "服务区外的车才能挪车!");
        }

        $now = time();
        if (time() - strtotime($order_info['end_time']) > 600){
            $this->throw_myexception(X_ERROR, "10分钟之内服务区外才能挪车!");
        }

        $order_count1 = (new UserReparkOrderModel())->get_user_unfinished_order(ApiOper::instance()->userinfo()['id']);
        if ($order_count1 > 0) {
            $this->throw_myexception(X_ERROR, "您有进行中挪车的订单!");
        }

        $ret = (new UserReparkOrderModel())->submit($bike_info['city_id'], $bike_info,$this->argv['order_id'], $this->argv['bluetooth'], $this->argv['unlock']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function user_repark_finishAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'park_id' => 'optstring',
            'city_id' => 'optstring',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);


        $ret = (new UserReparkOrderModel())->finish($this->city_id, $this->argv['order_id'], $this->argv['park_id'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function user_repark_trackAction(){
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $info = (new UserReparkOrderModel())->query_info($this->city_id, $this->argv['order_id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }


    function cs_repark_submitAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'bluetooth' => ['optint', 0],
            'unlock' => ['optint', 1],
        );

        $this->check_params();
        $this->check_token();



        $order_info = (new UserRadingOrderModel())->query_info_by_city_id($this->city_id, $this->argv['order_id'],false);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }


        $lock = new DLock($order_info['bike_id'], 60);
        $bike_info = BikeModel::instance()->get_info_by_id($order_info['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败!");
        }

        if ($bike_info['last_order_id'] != $this->argv['order_id']){
            $this->throw_myexception(X_ERROR, "该车辆已经被他人骑行,无法挪车!");
        }




        if ($order_info['order_state'] != RIDE_ORDER_STATE_FINISH) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单未结束!");
        }

        if (!empty($order_info['end_svca_id'])){
            $this->throw_myexception(ERROR_STATE_DISMATCH, "服务区外的车才能挪车!");
        }

        $order_count = (new UserRadingOrderModel())->get_user_unfinished_order($order_info['user_id']);
        if ($order_count > 0) {
            $this->throw_myexception(X_ERROR, "用户有进行中的骑行的订单!");
        }



        $now = time();
//        if (time() - strtotime($order_info['end_time']) > 600){
//            throw new MyException(X_ERROR, "10分钟之内服务区外才能挪车!");
//        }

        $order_count1 = (new UserReparkOrderModel())->get_user_unfinished_order($order_info['user_id']);
        if ($order_count1 > 0) {
            $this->throw_myexception(X_ERROR, "您有进行中挪车的订单!");
        }

        $ret = (new UserReparkOrderModel())->cs_submit($order_info,$bike_info['city_id'], $bike_info,$this->argv['order_id'], $this->argv['bluetooth'], $this->argv['unlock']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function cs_repark_finishAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'park_id' => 'optstring',
            'city_id' => 'optstring',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);


        $order_info = (new UserRadingOrderModel())->query_info_by_city_id($this->city_id, $this->argv['order_id'],false);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }


        $ret = (new UserReparkOrderModel())->finish($order_info['city_id'],$order_info['user_repark_order'], $this->argv['park_id'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }


    function hourly017Action() {
        $this->paramlist = [
            'today' => 'optstring',
            'city' => 'optstring',
        ];
        $this->check_params();
        $this->check_token();

        $city_id = $this->city_id;
        if (!empty($this->argv['city'])) {
            $city_id = $this->argv['city'];
            if ($city_id == 'ALL') {
                $city_id = null;
            }
        }

        if (!empty($city_id)) {
            $this->resp['today'] = (new UserRadingOrderModel())->hourly($city_id, date('Y-m-d'));
            $this->resp['yesterday'] = (new UserRadingOrderModel())->hourly($city_id, date('Y-m-d', strtotime('-1 day')));
            $this->resp['aweekago'] = (new UserRadingOrderModel())->hourly($city_id, date('Y-m-d', strtotime('-7 day')));
        } else {
            $this->resp['today'] = (new UserRadingOrderModel())->hourly_all_city(date('Y-m-d'));
            $this->resp['yesterday'] = (new UserRadingOrderModel())->hourly_all_city(date('Y-m-d', strtotime('-1 day')));
            $this->resp['aweekago'] = (new UserRadingOrderModel())->hourly_all_city(date('Y-m-d', strtotime('-7 day')));
        }

        return $this->ret_resp();
    }

}
